package com.honeyms.project.system.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeyms.framework.shiro.service.RegisterService;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.framework.web.domain.AjaxResult;
import com.honeyms.project.blog.blogcontent.service.IBlogContentService;
import com.honeyms.project.im.email.service.EmailService;
import com.honeyms.project.system.config.service.IConfigService;
import com.honeyms.project.system.user.domain.User;

/**
 * 注册验证
 * 
 * @author honeyms
 */
@Controller
public class RegisterController extends BaseController
{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private RegisterService registerService;

    @Autowired
    private IConfigService configService;
    
    @Autowired
    private IBlogContentService blogContentService;
    
    @Autowired
	private EmailService emailService;

    @GetMapping("/register")
    public String register(ModelMap mmap)
    {
    	mmap.put("RegistrationAgreement", blogContentService.selectBlogContentById(1L));
    	if(getSysUser()!=null) {
        	return "redirect:/";
        }
        return "register";
    }

    //@Log(title = "新用户注册", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    @ResponseBody
    public AjaxResult ajaxRegister(User user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        boolean flag = StringUtils.isEmpty(msg);
        if (flag) {
        	try {
				if("true".equals(configService.selectConfigByKey("sys.blog.email"))) {
					String localurl=configService.selectConfigByKey("sys.blog.url");
					String  emailtext="好呢博客用户:&nbsp;<a href='"+localurl+"/blog/open/otherpersonalmain/"+user.getUserId()+"' "
							+ "title='查看我的对外访问主页!'>"+user.getLoginName()+"</a>&nbsp;你好!<br>&emsp;&emsp;&emsp;欢迎你加入好呢博客!<br>点击👉<a "
							+ "href='http://huangwenlong.wicp.vip' title='好呢博客'>HoneyBlog</a>去查看!";
					emailService.sendhtmlemail(user.getEmail(), emailtext);
					System.out.println(emailtext);
					String adminemailtext="尊敬的管理员!<br>用户:<a href='"+localurl+"/blog/open/otherpersonalmain/"+user.getUserId()+"' title='查看他的主页!'>"+user.getLoginName()+"</a>在好呢博客注册了账号!";
					emailService.sendhtmlemail("506486635@qq.com", adminemailtext);
				}
				logger.info("发送注册成功邮件： {} => 发送成功!", user.getLoginName());
			} catch (Exception e) {
				logger.info("发送注册成功邮件： {} => 发送失败!  原因:"+e.toString(), user.getLoginName());
			}
		}
        return flag ? success() : error(msg);
    }
}
